package com.irain.pp.tool;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.irain.pp.tool.exception.JsonTransferException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * JSON转化工具类
 * @author WangYanPeng
 * @date 2017/10/16 17:42
 */
public class JavaBeanUtils {

    private JavaBeanUtils(){}

    private final static ObjectMapper NON_NULL_MAPPER = new ObjectMapper()
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);

    public final static String EMPTY_JSON_TEXT = "{}";


    public static<T> String toJsonString(T javaBean){
        try {
            return NON_NULL_MAPPER.writeValueAsString(javaBean);
        } catch (JsonProcessingException e) {
            throw new JsonTransferException(e);
        }
    }

    public static <T> JsonNode toJsonNode(T javaBean){
        return NON_NULL_MAPPER.valueToTree(javaBean);
    }

    public static <T> T convertValue(Object fromValue,Class<T> toValueClass){
        return NON_NULL_MAPPER.convertValue(fromValue,toValueClass);
    }

    public static  <T> T convertValue(Object fromValue,TypeReference toValueTypeRef){
        return NON_NULL_MAPPER.convertValue(fromValue,toValueTypeRef);
    }

    public static  <T> T convertValueByType(Object fromValue,Type toValueType){
        return NON_NULL_MAPPER.convertValue(fromValue, NON_NULL_MAPPER.constructType(toValueType));
    }

    public static <T> Map<String,Object> toMap(T javaBean){
        JsonNode jsonNode = NON_NULL_MAPPER.valueToTree(javaBean);
        Map<String,Object> result = new HashMap<>(16);
        if (jsonNode.size() != 0) {
            Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields();
            while (iterator.hasNext()) {
                Map.Entry<String, JsonNode> entry = iterator.next();
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result;
    }


    public static <T> String toUrlParams(T javaBean){
        JsonNode jsonNode = NON_NULL_MAPPER.valueToTree(javaBean);
        if (jsonNode.size() == 0){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields();
        while (iterator.hasNext()){
            Map.Entry<String, JsonNode> entry = iterator.next();
            sb.append(entry.getKey()).append('=');
            if (entry.getValue().isTextual()){
                sb.append(entry.getValue().textValue()).append('&');
            } else {
                sb.append(entry.getValue()).append('&');
            }
        }
        return sb.substring(0,sb.length() - 1);
    }


}
